
//设置TIM5_CH2为pwm输出，默认占空比为0，修改占空比就是修改TIM_Pulse
void MX_TIM5_OC_CH2_PA1_Init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef        TIM_OCInitStructure;
    GPIO_InitTypeDef         GPIO_InitStructure;

    // ========================
    // 1. 时钟使能
    // ========================
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);

    // ========================
    // 3. 配置 PA1 为复用推挽输出（TIM5_CH2）
    // ========================
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  // 上拉输入
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
	
		

    // ========================
    // 4. 定时器基础配置
    // ========================
    // 假设系统时钟 72MHz
    // 预分频：71 → 计数器时钟 = 72MHz / 72 = 1MHz → 1us 计数一次
    TIM_TimeBaseStructure.TIM_Prescaler = 71;
    TIM_TimeBaseStructure.TIM_Period = 0x766;           // 自动重载值（十进制1894）
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
																																	
    TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);

    // ========================
    // 5. 输出比较配置 (TIM5_CH2)
    // ========================
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;                     
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;          
    TIM_OCInitStructure.TIM_Pulse = 0;      // 十进制947
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC2Init(TIM5, &TIM_OCInitStructure);
		TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Disable);


    // ========================
    // 7. 启动定时器
    // ========================
    TIM_Cmd(TIM5, ENABLE);
}



//设置TIM3_CH2为输入捕获，还需要去stm32f10x_it.c中实现TIM3_IRQHandler中断处理函数
void MX_TIM3_IC_CH2_PC7_Init(void)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_ICInitTypeDef        TIM_ICInitStructure;
    GPIO_InitTypeDef         GPIO_InitStructure;

    // ========================
    // 1. 时钟使能
    // ========================
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);  // 必须开启 AFIO
		GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);//要在配置GPIO前重映射

    // ========================
    // 3. 配置 PC7 为浮空输入（TIM3_CH2）
    // ========================
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  // 上拉输入 不清楚外部器件是推挽还是开漏输出，就设置为上拉输入
																										//如果外部器件是推挽也不影响功能，如果是开漏就可以保证输入信号的稳定
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
	
		

    // ========================
    // 4. 定时器基础配置
    // ========================
    // 假设系统时钟 72MHz
    // 预分频：71 → 计数器时钟 = 72MHz / 72 = 1MHz → 1us 计数一次
    TIM_TimeBaseStructure.TIM_Prescaler = 71;
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;           // 自动重载值（65535）
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
		TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);																															

    // ========================
    // 5. 输入捕获配置 (TIM3_CH2)
    // ========================
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;                     // 通道2
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;          // 初始下降沿触发，因为红外接收头空闲时发出高电平
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;      // 直接映射
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0x0;
    TIM_ICInit(TIM3, &TIM_ICInitStructure);

    // ========================
    // 6. 使能中断
    // ========================
    TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);  // 使能通道2捕获中断
		TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

    // ========================
    // 7. 启动定时器
    // ========================
    TIM_Cmd(TIM3, ENABLE);
}
void TIM3_NVIC_Config(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}